package ruixinyi.experiment12.service;
import org.springframework.stereotype.Service;
import ruixinyi.experiment12.model.Student;

import java.util.ArrayList;
import java.util.List;

@Service
public class StudentService {
    private List<Student> students = new ArrayList<>();

    public StudentService() {
        // 初始化一些学生数据
        students.add(new Student(1, "cc", "c", "cc.doe@example.com", 20));
        students.add(new Student(2, "ll", "l", "ll.doe@example.com", 22));
    }

    public List<Student> findAllStudents() {
        return students;
    }

    public Student createStudent(Student student) {
        student.setId( (students.size() + 1));
        students.add(student);
        return student;
    }

    public Student findStudentById(Integer id) {
        return students.stream()
                .filter(student -> student.getId().equals(id))
                .findFirst()
                .orElse(null);
    }

    public Student updateStudent(Integer id, Student studentDetails) {
        Student student = findStudentById(id);
        if (student != null) {
            student.setFirstName(studentDetails.getFirstName());
            student.setLastName(studentDetails.getLastName());
            student.setEmail(studentDetails.getEmail());
            student.setAge(studentDetails.getAge());
        }
        return student;
    }

    public void deleteStudent(Integer id) {
        students.removeIf(student -> student.getId().equals(id));
    }
}